//
//  PayOrderViewController.m
//  Youhuiju
//
//  Created by 余尚飞 on 16/8/12.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "PayOrderViewController.h"

#import "PaySuccessfulViewController.h"

//自定义视图
#import "PayOrderTopView.h"

//自定义cell
#import "PayOrderCostTableViewCell.h"//订单金额
#import "PayOrderChoosePayTableViewCell.h"//选择支付方式
#import "PayOrderPayTableViewCell.h"//支付方式
#import "PayOrderTableViewCell.h"//支付


@interface PayOrderViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)PayOrderTopView *topView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

//判断点击状态
@property(nonatomic,assign)NSInteger flag;//判断是否有选中
@property(nonatomic,assign)NSInteger num2;//判断第一个是否有选中
@property(nonatomic,assign)NSInteger num1;//判断第二个是否有选中

@end

static NSString *PayOrderCostTableViewCellIden = @"PayOrderCostTableViewCellIden";
static NSString *PayOrderChoosePayTableViewCellIden = @"PayOrderChoosePayTableViewCellIden";
static NSString *PayOrderPayTableViewCellIden = @"PayOrderPayTableViewCellIden";
static NSString *PayOrderTableViewCellIden = @"PayOrderTableViewCellIden";

@implementation PayOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self load_UI];
}

#pragma mark ------- load_UI
-(void)load_UI
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self topView];
    
    self.flag = 0;
    self.num2 = 0;
    self.num1 = 0;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"PayOrderCostTableViewCell" bundle:nil] forCellReuseIdentifier:PayOrderCostTableViewCellIden];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"PayOrderChoosePayTableViewCell" bundle:nil] forCellReuseIdentifier:PayOrderChoosePayTableViewCellIden];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"PayOrderPayTableViewCell" bundle:nil] forCellReuseIdentifier:PayOrderPayTableViewCellIden];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"PayOrderTableViewCell" bundle:nil] forCellReuseIdentifier:PayOrderTableViewCellIden];
}

#pragma mark -------- tableView delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1)
    {
        return 3;
    }
    else
    {
        return 1;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 2)
    {
        return 0;
    }
    
        return 10 * FitH;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return 44 * FitH;
    }
    else if(indexPath.section == 1)
    {
        return 60 * FitH;
    }
    else
    {
        return 84 * FitH;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WeakObj(self);
    switch (indexPath.section)
    {
        case 0:
        {
            PayOrderCostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PayOrderCostTableViewCellIden forIndexPath:indexPath];
            cell.allCost = self.userAllCost;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
            break;
        case 1:
        {
            if (indexPath.row == 0)
            {
                
                PayOrderChoosePayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PayOrderChoosePayTableViewCellIden forIndexPath:indexPath];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;

                return cell;
            }
            else if(indexPath.row == 1)
            {
                PayOrderPayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PayOrderPayTableViewCellIden forIndexPath:indexPath];
                cell.payWayImg.image = LoadImage(@"zhifubao");
                
                cell.selectionStyle = UITableViewCellSelectionStyleNone;

                return cell;
            }
            else
            {
                PayOrderPayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PayOrderPayTableViewCellIden forIndexPath:indexPath];
                cell.payWayImg.image = LoadImage(@"weixin");
                cell.payTitle.text = @"微信";
                cell.pryDetailLabel.text = @"微信";
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                
                return cell;
            }
        }
            break;
        case 2:
        {
            PayOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PayOrderTableViewCellIden forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.buttonClickedHandler = ^{
            
                DLog(@"支付-------%ld",self.flag);
                if (weakself.flag == 1)
                {
                    DLog(@"调用支付接口");
                    PaySuccessfulViewController *pvc = [[PaySuccessfulViewController alloc]initWithNibName:@"PaySuccessfulViewController" bundle:nil];
                    pvc.model = weakself.model;
                    [weakself.navigationController pushViewController:pvc animated:YES];
                    
                }
                else
                {
                    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"至少要选择一种支付方式" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                    [alert show];
                }
            };

            return cell;
        }
            break;
        default:
            return nil;
    }
   
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.section )
    {
        case 0:
        {
            
        }
            break;
        case 1:
        {
            if (indexPath.row == 1)
            {
                DLog(@"支付宝------");
                PayOrderPayTableViewCell *cell =  [tableView cellForRowAtIndexPath:indexPath];
                
                NSIndexPath *indexP = [NSIndexPath indexPathForRow:2 inSection:1];
                PayOrderPayTableViewCell *cell1 =  [tableView cellForRowAtIndexPath:indexP];
                
                if (self.num1 == 0)
                {
//                    cell.ChooseBtn.backgroundColor = [UIColor blackColor];
                    [cell.ChooseBtn setBackgroundImage:LoadImage(@"choose") forState:UIControlStateNormal];
//                    cell1.ChooseBtn.backgroundColor = [UIColor colorWithHexString:@"ff5f7b"];
                    [cell1.ChooseBtn setBackgroundImage:LoadImage(@"unChoose") forState:UIControlStateNormal];
                   
                    self.flag = 1;
                    self.num1 = 1;
                    self.num2 = 0;
                }else
                {
//                    cell.ChooseBtn.backgroundColor = [UIColor colorWithHexString:@"ff5f7b"];
//                    cell1.ChooseBtn.backgroundColor = [UIColor blackColor];
//                    [cell1.ChooseBtn setBackgroundImage:LoadImage(@"unChoose") forState:UIControlStateNormal];
                    //                    cell1.ChooseBtn.backgroundColor = [UIColor colorWithHexString:@"ff5f7b"];
                    [cell.ChooseBtn setBackgroundImage:LoadImage(@"unChoose") forState:UIControlStateNormal];
                    self.flag = 0;
                    self.num1 = 0;
                }
                
//                PayOrderPayTableViewCell *cell2 =  [tableView cellForRowAtIndexPath:indexPath ];
//                cell.ChooseBtn.backgroundColor = [UIColor blackColor];
//                self.flag = 0;
            }
            else if(indexPath.row == 2)
            {
                DLog(@"微信------");
                PayOrderPayTableViewCell *cell =  [tableView cellForRowAtIndexPath:indexPath];
                NSIndexPath *indexP = [NSIndexPath indexPathForRow:1 inSection:1];
                PayOrderPayTableViewCell *cell1 =  [tableView cellForRowAtIndexPath:indexP];
//                if (cell.ChooseBtn.backgroundColor != [UIColor blackColor])
                if (self.num2 == 0)//判断是否选中
                {
//                    cell.ChooseBtn.backgroundColor = [UIColor blackColor];
//                    cell1.ChooseBtn.backgroundColor = [UIColor colorWithHexString:@"ff5f7b"];
                    [cell.ChooseBtn setBackgroundImage:LoadImage(@"choose") forState:UIControlStateNormal];
                    //                    cell1.ChooseBtn.backgroundColor = [UIColor colorWithHexString:@"ff5f7b"];
                    [cell1.ChooseBtn setBackgroundImage:LoadImage(@"unChoose") forState:UIControlStateNormal];
                    
                    self.flag = 1;
                    self.num2 = 1;
                    self.num1 = 0;
                }else
                {
//                    cell.ChooseBtn.backgroundColor = [UIColor colorWithHexString:@"ff5f7b"];
//                    [cell1.ChooseBtn setBackgroundImage:LoadImage(@"unChoose") forState:UIControlStateNormal];
                    //                    cell1.ChooseBtn.backgroundColor = [UIColor colorWithHexString:@"ff5f7b"];
                    [cell.ChooseBtn setBackgroundImage:LoadImage(@"unChoose") forState:UIControlStateNormal];
//                    cell1.ChooseBtn.backgroundColor = [UIColor blackColor];
                    self.flag = 0;
                    self.num2 = 0;
                }
                
            }
            else
            {
//                NSIndexPath *indexP = [NSIndexPath indexPathForRow:1 inSection:1];
//                PayOrderPayTableViewCell *cell =  [tableView cellForRowAtIndexPath:indexP];
//                
//                NSIndexPath *indexP1 = [NSIndexPath indexPathForRow:2 inSection:1];
//                PayOrderPayTableViewCell *cell1 =  [tableView cellForRowAtIndexPath:indexP1];
//                
//                if ((cell1.ChooseBtn.backgroundColor == [UIColor blackColor]) || (cell.ChooseBtn.backgroundColor = [UIColor blackColor]))
//                {
//                    DLog(@"调用支付接口");
//                }
//                else
//                {
//                    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"至少要选择一种支付方式" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
//                    [alert show];
//                }

                
            }
                
        }
            break;
        case 2:
        {
            
        }
            break;
        default:
            break;
    }

}

#pragma mark ----- lazy loading
-(PayOrderTopView *)topView
{
    
    WeakObj(self);
    if (!_topView)
    {
        _topView = [[PayOrderTopView alloc]init];
        [self.view addSubview:_topView];
        [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.mas_left);
            make.right.equalTo(self.view.mas_right);
            make.top.equalTo(self.view.mas_top).offset(20);
            make.height.equalTo(@44);
        }];
        _topView.backButtonClickedHandler = ^{
        
            [weakself.navigationController popViewControllerAnimated:YES];
        };
    }
    return _topView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];



}



@end
